package com.example.exam.service;

import com.example.exam.exception.ServiceException;
import com.example.exam.mapper.BookMapper;
import com.example.exam.pojo.Book;
import com.example.exam.pojo.BookPager;
import com.example.exam.service.imp.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class MyBookService  implements BookService {

   @Autowired
   BookMapper bookMapper;


    @Override
    public void insertBook(Book book) {
        int i=bookMapper.insertBook(book);
        if(i==0){
            throw  new ServiceException("书本插入失败");
        }
    }

    @Override
    public void updateBookById(Book book) {
        int i=bookMapper.updateBookById(book);
        if(i==0){
            throw  new ServiceException("书本更新失败");
        }
    }

    @Override
    public BookPager selectBookByLimit(Long row, Long number) {
        long count = bookMapper.countRow();
        long rows = count/number;
        if(count%number!=0){
            rows++;
        }
        BookPager bookPager = new BookPager();
        bookPager.setBooks(bookMapper.selectBookByLimit((row-1)*number,number));
        bookPager.setCount(count);
        bookPager.setRows(rows);
        return bookPager;
    }

    @Override
    public void deleteBookById(Long id) {
        int i=bookMapper.deleteBookById(id);
        if(i==0){
            throw  new ServiceException("书本删除失败");
        }
    }
}
